<?php

if(defined("_PARA_IPBLOCK"))
    return;
define("_PARA_IPBLOCK", 1);

class IPBlock {
    public $id;
    public $name;
    public $createdate;
    public $definition;
    public $message;
    public $block_logged_in;
    public $block_viewing;
    public $active;

    public $definition_ok = false;
    
    public function IPBlock( $id, $name, $createdate, 
                             $definition, $message,
                             $block_logged_in, $block_viewing, $active ) {
        $this->id              = $id;
        $this->name            = $name;
        $this->createdate      = $createdate;
        $this->definition      = $definition;
        $this->message         = $message;
        $this->block_logged_in = $block_logged_in;
        $this->block_viewing   = $block_viewing;
        $this->active          = $active;

        $this->definition_ok = false;
        if( self::isLegalDefinition( $this->definition ) )
            $this->definition_ok = true;
    }


    public function check_ip( $ip ) {
        if(!$this->definition_ok )
            return 'FAIL';

        if(!preg_match('/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/', $ip, $imatches))
            return 'FAIL';

        if(!preg_match('/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(.*)$/', $this->definition, $dmatches))
            return 'FAIL';
        
        if( ($imatches[1] != $dmatches[1]) || ($imatches[2] != $dmatches[2]) || ($imatches[3] != $dmatches[3]) )
            return 'OK';

        $d4 = $dmatches[4];
        if( $d4 == '*' )
            return 'BLOCKED';
        elseif( preg_match('/^\d{1,3}$/', $d4 )  && ( $d4 == $imatches[4] ))
            return 'BLOCKED';
        elseif( preg_match('/^(\d{1,3})\-(\d{1,3})$/', $d4, $range )) {
            $i4 = (int) $imatches[4];
            if( ($i4 >= (int)$range[1] ) && ( $i4 <= (int)$range[2]) )
                return 'BLOCKED';
        }

        return 'OK';
    }


    // ip block definitions can have 3 differnt formats:
    // 1. single ip i.e: 129.76.23.20
    // 2. wildcard on last digit: 129.76.23.*
    // 3. range on last digit: 129.76.23.10-20
    public static function isLegalDefinition( $def ) {
        if(!preg_match('/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.([0-9-\*]+)$/', $def, $matches))
            return false;

        $d1 = (int)$matches[1];
        $d2 = (int)$matches[2];
        $d3 = (int)$matches[3];


        if(($d1 < 0) || ($d1 > 255 ))
            return false;
        if(($d2 < 0) || ($d2 > 255 ))
            return false;
        if(($d3 < 0) || ($d3 > 255 ))
            return false;


        $m4 = $matches[4];

        if( $m4 == '*' )
            return true;
        elseif( preg_match('/^\d{1,3}$/', $m4 )) {
            $m4 = (int)$m4;
            if( ($m4 < 1) || ($m4 > 255) )
                return false;
            return true;
        }
        elseif( preg_match('/^(\d{1,3})\-(\d{1,3})$/', $m4, $matches )) {
            $d1 = (int)$matches[1];
            $d2 = (int)$matches[2];
            if( ( $d1 < 1) || ( $d1 > 255) )
                return false;
            if( ( $d2 < 1) || ( $d2 > 255) )
                return false;
            return true;
        }

        return false;

    }
}

?>